/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as _ from "lodash"
import httpClient from "../httpclient"

export class CustomerAddress {
  public id: string = ""
  private first_name: string = ""
  private last_name: string = ""
  public mobile: string = ""
  private country_code: string = ""
  public province_id: string = ""
  public province: string = ""
  public city_id: string = ""
  public city: string = ""
  public county_id: string = ""
  public county: string = ""
  public address: string = ""
  public defaulted: boolean = false
  private created_time: string = ""
  public zip: string = ""
  public tag: string = ""

  public get firstName() {
    return this.first_name
  }

  public set firstName(firstName) {
    this.first_name = firstName
  }

  public get lastName() {
    return this.last_name
  }

  public set lastName(lastName) {
    this.last_name = lastName
  }

  public get countryCode() {
    return this.country_code
  }

  public set countryCode(countryCode: string) {
    this.country_code = countryCode
  }

  public get provinceId() {
    return this.province_id
  }

  public set provinceId(provinceId: string) {
    this.province_id = provinceId
  }

  public get cityId() {
    return this.city_id
  }

  public set cityId(cityId: string) {
    this.city_id = cityId
  }

  public get countyId() {
    return this.county_id
  }

  public set countyId(countyId: string) {
    this.county_id = countyId
  }

  public get createdTime() {
    return this.created_time
  }
}

export class CustomerAddressServiceV1 {

  public static removeAddress(customerId: string, addressId: string): Promise<void> {
    return httpClient.delete(`/v1/customers/${customerId}/addresses/${addressId}`)
      .then(({ data }) => data)
  }

  public static addAddress(customerId: string, address: CustomerAddress): Promise<void> {
    return httpClient.post(`/v1/customers/${customerId}/addresses`, address)
      .then(({ data }) => data)
  }

  public static setAddress(customerId: string, address: CustomerAddress): Promise<void> {
    return httpClient.patch(`/v1/customers/${customerId}/addresses/${address.id}`, address)
      .then(({ data }) => data)
  }

  public static getAddresses(customerId: string): Promise<CustomerAddress[]> {
    return httpClient.get(`/v1/customers/${customerId}/addresses`)
      .then(({ data }) => _.map(data, address => _.assign(new CustomerAddress(), address)))
  }

  public static getAddress(customerId: string, addressId: string): Promise<CustomerAddress> {
    return httpClient.get(`/v1/customers/${customerId}/addresses/${addressId}`)
      .then(({ data }) => _.assign(new CustomerAddress(), data))
  }

  public static getDefaultAddress(customerId: string): Promise<CustomerAddress> {
    return httpClient.get(`/v1/customers/${customerId}/addresses/default`)
      .then(({ data }) => _.assign(new CustomerAddress(), data))
  }
}
